package cn.itsource._02workqueues;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class HelloCustomer {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 创建连接通过工厂获得
        ConnectionFactory factory = new ConnectionFactory();
        Connection connection = factory.newConnection();
        // 创建通道
        Channel channel = connection.createChannel();

        // 每次处=最多堆积1个
        channel.basicQos(1);
        // 处理消息
        Consumer callback = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("拿到消息：" + new String(body));
//                int i =1/0;
                System.out.println("consumerTag:" + consumerTag);
                System.out.println("消息队列："+ envelope.getRoutingKey());
                System.out.println("交换机"+ envelope.getExchange());
                System.out.println("消息的ID："+envelope.getDeliveryTag());
                System.out.println("消息消费完毕");
                long deliveryTag = envelope.getDeliveryTag();
                // 手动签收，第一个参数消息的id，是否批量签收
                channel.basicAck(deliveryTag, false);
            }
        };
        // 监听队列，第一个参数监听的队列名，第二个参数手动接收，第三个参数回调
        channel.basicConsume(HelloRabbitmq.QUEUE, false, callback);


    }
}
